#include "mapscreenState.h"


//The state displaying the map. Allow the user to choose explored regions.

mapscreenState mapscreenState::m_mapscreenState;

	void mapscreenState::Init(sf::RenderWindow& window){
	    //stuff
    std::cout << "mapScreenState Started" << std::endl ;
    testRect.setSize(sf::Vector2f(window.getSize().x,window.getSize().y));
    testRect.setPosition(sf::Vector2f(0,0));
    testRect.setFillColor(sf::Color::Blue);

    // +++ TGUI Stuff +++
    gui.setWindow(window);
    gui.setGlobalFont("resources/arial.ttf");

    int mapListSize = AManOfMapsState::getMapListSize();

    //create icons for each of the maps in the maplist
    for(int i=0;i<mapListSize;i++){
        sf::RectangleShape tempRect;
        tempRect.setSize(sf::Vector2f(50,50));
        tempRect.setPosition(sf::Vector2f(100*i + 100,200));
        tempRect.setFillColor(sf::Color::Red);
        mapIcons.push_back(tempRect);
    }

    // VIEW
    view.setSize(window.getSize().x, window.getSize().y);
    view.setViewport(sf::FloatRect(0,0,1,1));
    std::cout  << "Window Size(px): " << view.getSize().x << " " << view.getSize().y << std::endl;

    //Border Image

    if(!mapBorderTexture.loadFromFile("resources/map4.png")){
        std::cout << "Map Border Texture did not load";
    }

    mapBorder.setTexture(mapBorderTexture);
    mapBorder.scale(1.0,1.0);
    mapBorder.setPosition(0,0);

    }

    //run when state is closed
	void mapscreenState::Cleanup(){
	    std::cout << "mapScreenState Ended" << std::endl ;
	}

	void mapscreenState::Pause(){
	    //stuff
	}

	void mapscreenState::Resume(){
	}

	void mapscreenState::Events(GameEngine* game, sf::RenderWindow& window){
    while (window.pollEvent(event)){

        // Close the window and end the game when X clicked
        if (event.type == sf::Event::Closed){
            game->Quit();
        }

        //handle tGUI events
        gui.handleEvent(event);

        // The callback loop
        tgui::Callback callback;

        while (gui.pollCallback(callback)){
            // Change the tile where the player is standing
            if (callback.id == 1){

            }

            if (callback.id == 2){

            }

            if (callback.id == 3){

            }

        }

        // === KEYBOARD EVENTS ===
        if (event.type == sf::Event::KeyPressed ){

            //RETURN KEY
            if (event.key.code == sf::Keyboard::Return){
            }

            //ESCAPE KEY
            if (event.key.code == sf::Keyboard::Escape){
                game->PopState();
            }

            //Move Keys
            if (event.key.code == sf::Keyboard::D || event.key.code == sf::Keyboard::A  || event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::S){

            }
        }


        // === SINGLE MOUSE EVENTS ===
        if (event.type == sf::Event::MouseButtonPressed){
            //When the player left clicks,
            if(event.mouseButton.button == sf::Mouse::Left){
                std::cout << "left click" << std::endl;

                //check which box you clicked in
                sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));

                //set current map to the selected icon and load the Mom gamestate
                for(int i = 0; i < mapIcons.size(); i++){
                        if(mapIcons[i].getGlobalBounds().contains(mousePosition.x, mousePosition.y)){
                            std::cout << "current:" << i << std::endl;
                            AManOfMapsState::currentMap = i;
                            mapIcons.clear();
                            game->PopState();
                        }
                }
            }
        }
    }
	}

	void mapscreenState::Update(GameEngine* game, sf::RenderWindow& window){
        //stuff
	}

	void mapscreenState::Render(GameEngine* game, sf::RenderWindow& window){

        window.setView(view);

        window.clear(sf::Color::White);

        window.draw(testRect);

        window.draw(mapBorder);

        for(int i=0;i<mapIcons.size();i++){
            window.draw(mapIcons[i]);
        }

        //window.draw(rect1);
        //window.draw(rect2);

        //gui.draw();

        window.display();
	}
